package com.skh.string;

import java.util.Arrays;

/**
 * 反转字符串
 * created by skh on 2018/8/25
 */
public class ReverseString {

	/*
	编写一个函数，其作用是将输入的字符串反转过来。

	示例 1:

	输入: "hello"
	输出: "olleh"
	示例 2:

	输入: "A man, a plan, a canal: Panama"
	输出: "amanaP :lanac a ,nalp a ,nam A"
	 */

	public String reverseString(String s) {
//		StringBuilder stringBuilder = new StringBuilder(s);
//		return stringBuilder.reverse().toString();

		char[] chars = s.toCharArray();
		char[] rs = new char[chars.length];

		for (int i=0;i<chars.length;i++) {
			rs[i] = chars[chars.length-i-1];
		}

		return new String(rs);
	}

	public String reverseString1(String s) {

		char[] chars = s.toCharArray();

		int left = 0;
		int right = chars.length-1;

		while (left < right) {
			char temp = chars[left];
			chars[left] = chars[right];
			chars[right] = temp;
			left++;
			right--;
		}
		return new String(chars);
	}
}
